﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace LibFriesland_Campina_CRM
{
    class DalInvoice
    {
        //Them mot hoa don moi
        public static long addInvoice(BizInvoice pInvoice)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                Invoice invoice = new Invoice();
                invoice.DateTimeInvoice = pInvoice.datetime_invoice;
                invoice.DateTimePhoneInvoice = pInvoice.datetime_phone_invoice;
                invoice.ByNA = pInvoice.by_na;
                invoice.ByShop = pInvoice.by_shop;
                invoice.ByShopper = pInvoice.by_shopper;
                invoice.SessionInvoice = pInvoice.session_invoice;
                db.Invoice.InsertOnSubmit(invoice);
                db.SubmitChanges();
                return invoice.InvoiceNumID;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Lay id cua hoa don theo session
        public static long getInvoiceNumID(string pSession_invoice)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var invoice = db.Invoice.Single(p => p.SessionInvoice == pSession_invoice.Trim());
                return invoice.InvoiceNumID;
            }
            catch
            {
                return -1;
            }
        }

        //Lay chi tiet hoa don cua mot hoa don
        public static List<BizInvoiceDetail> getInvoiceDetailbyInvoice(long pInvoice_numid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                var invoiceDetails = from item in db.InvoiceDetail
                                     where item.ByInvoice == pInvoice_numid
                                     select new BizInvoiceDetail(item.ByInvoice, item.ByProduct, item.Quantity);
                return invoiceDetails.ToList();
            }
            catch(Exception ex)
            {
                return null;
            }
        }

        //Xoa mot hoa don
        public static int deleteInvoice(long pInvoice_numid)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                List<BizInvoiceDetail> invoiceDetails = getInvoiceDetailbyInvoice(pInvoice_numid);
                //Xoa InvoiceDatail
                foreach (var item in invoiceDetails)
                {
                    BizInvoiceDetail.deleteInvoiceDetail(item.by_invoice, item.by_product);
                }

                var invoice = db.Invoice.Single(p => p.InvoiceNumID == pInvoice_numid);
                db.Invoice.DeleteOnSubmit(invoice);
                db.SubmitChanges();
                return 1;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }

        #region Dung cho Webservice
        //Them hoa don cho Shopper
        public static int addInvoicebyShopper(BizShopper pShopper, BizNA pNA, BizInvoice pInvoice, BizInvoiceDetail pInvoiceDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                //Doi voi shopper moi
                if (pShopper.shopper_numid == -1)
                {
                    addInvoicebyNewShopper(pShopper, pNA, pInvoice, pInvoiceDetail);
                }
                //Doi voi shopper vang lai
                else if (pShopper.shopper_numid == 1)
                {
                    addInvoicebyNonShopper(pShopper, pNA, pInvoice, pInvoiceDetail);
                }
                else //pShopper.shopper_numid != 1
                {
                    addInvoicebyOldShopper(pShopper, pNA, pInvoice, pInvoiceDetail);
                }

                //Cap nhat lai so luong hang trong Shop
                BizProductInShop.updateProductQuantityInShop(pInvoiceDetail.by_product, pInvoice.by_shop, (-1) * pInvoiceDetail.quantity);

                //Cap nhat lai so luong hang cua cua hang trong nhat ki hang
                BizProductInShopDiary.updateProductQuantityInShopDiary(pInvoiceDetail.by_product, pInvoice.by_shop, Convert.ToDateTime(pInvoice.datetime_phone_invoice), (-1) * pInvoiceDetail.quantity);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        //Them hoa don cho Shopper
        public static int addInvoicebyShopper(BizShopper pShopper, BizRelatives pRelatives, BizNA pNA, BizInvoice pInvoice, BizInvoiceDetail pInvoiceDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                //Doi voi shopper moi
                if (pShopper.shopper_numid == -1)
                {
                    addInvoicebyNewShopper(pShopper, pRelatives, pNA, pInvoice, pInvoiceDetail);
                }
                //Doi voi shopper vang lai
                else if (pShopper.shopper_numid == 1)
                {
                    addInvoicebyNonShopper(pShopper, pNA, pInvoice, pInvoiceDetail);
                }
                else //pShopper.shopper_numid != 1
                {
                    addInvoicebyOldShopper(pShopper, pNA, pInvoice, pInvoiceDetail);
                }

                //Cap nhat lai so luong hang trong Shop
                BizProductInShop.updateProductQuantityInShop(pInvoiceDetail.by_product, pInvoice.by_shop, (-1) * pInvoiceDetail.quantity);

                //Cap nhat lai so luong hang cua cua hang trong nhat ki hang
                BizProductInShopDiary.updateProductQuantityInShopDiary(pInvoiceDetail.by_product, pInvoice.by_shop, Convert.ToDateTime(pInvoice.datetime_phone_invoice), (-1) * pInvoiceDetail.quantity);
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        //Them hoa don cho Shopper moi
        private static long addInvoicebyNewShopper(BizShopper pShopper, BizNA pNA, BizInvoice pInvoice, BizInvoiceDetail pInvoiceDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                long currInvoiceNumID = getInvoiceNumID(pInvoice.session_invoice);
                if (currInvoiceNumID == -1) //Neu chua co Invoice
                {
                    //Them shopper moi
                    long shopper_numid = pShopper.addShopper();

                    //Tao thong tin cho Invoice
                    pInvoice.by_shopper = shopper_numid;
                    if (pNA.na_charid != "") //NA ban hang
                    {
                        pInvoice.by_na = pNA.na_charid;
                        pInvoice.by_shop = pNA.by_shop;
                    }
                    else //ShopOwner ban hang
                    {
                        pInvoice.by_na = "NADEFAULT";
                        pInvoice.by_shop = pNA.by_shop;
                    }

                    currInvoiceNumID = pInvoice.addInvoice();
                }

                //Tao chi tiet hoa don
                pInvoiceDetail.by_invoice = currInvoiceNumID;
                pInvoiceDetail.price = 0;
                pInvoiceDetail.addInvoiceDetail();
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Them hoa don doi voi Shopper moi
        private static long addInvoicebyNewShopper(BizShopper pShopper, BizRelatives pRelatives, BizNA pNA, BizInvoice pInvoice, BizInvoiceDetail pInvoiceDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                long currInvoiceNumID = getInvoiceNumID(pInvoice.session_invoice);
                if (currInvoiceNumID == -1) //Neu chua co Invoice
                {
                    //Them shopper moi
                    long shopper_numid = pShopper.addShopper();

                    //Them than nhan cho Shopper
                    pRelatives.by_shopper = shopper_numid;
                    pRelatives.addRelatives();

                    //Tao thong tin cho Invoice
                    pInvoice.by_shopper = shopper_numid;
                    if (pNA.na_charid != "") //NA ban hang
                    {
                        pInvoice.by_na = pNA.na_charid;
                        pInvoice.by_shop = pNA.by_shop;
                    }
                    else //ShopOwner ban hang
                    {
                        pInvoice.by_na = "NADEFAULT";
                        pInvoice.by_shop = pNA.by_shop;
                    }

                    currInvoiceNumID = pInvoice.addInvoice();
                }

                //Tao chi tiet hoa don
                pInvoiceDetail.by_invoice = currInvoiceNumID;
                pInvoiceDetail.price = 0;
                pInvoiceDetail.addInvoiceDetail();
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Them hoa don doi voi Shopper vang lai
        private static int addInvoicebyNonShopper(BizShopper pShopper, BizNA pNA, BizInvoice pInvoice, BizInvoiceDetail pInvoiceDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                long currInvoiceNumID = getInvoiceNumID(pInvoice.session_invoice);
                if (currInvoiceNumID == -1) ///Neu chua co Invoice
                {
                    //Tao thong tin cho Invoice
                    pInvoice.by_shopper = 1; //shopper_numid vang lai mac dinh la 1
                    if (pNA.na_charid != "") //NA ban hang
                    {
                        pInvoice.by_na = pNA.na_charid;
                        pInvoice.by_shop = pNA.by_shop;
                    }
                    else //ShopOwner ban hang
                    {
                        pInvoice.by_na = "NADEFAULT";
                        pInvoice.by_shop = pNA.by_shop;
                    }

                    currInvoiceNumID = pInvoice.addInvoice();
                }

                //Tao chi tiet hoa don
                pInvoiceDetail.by_invoice = currInvoiceNumID;
                pInvoiceDetail.price = 0;
                pInvoiceDetail.addInvoiceDetail();
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        //Them hoa don doi voi Shopper quen thuoc
        private static int addInvoicebyOldShopper(BizShopper pShopper, BizNA pNA, BizInvoice pInvoice, BizInvoiceDetail pInvoiceDetail)
        {
            Friesland_Campina_CRM_DataContext db = new Friesland_Campina_CRM_DataContext(new MySqlConnection(Thuvien.getConnString()));
            try
            {
                long currInvoiceNumID = getInvoiceNumID(pInvoice.session_invoice);
                if (currInvoiceNumID == -1) //Neu chua co Invoice
                {
                    //Lay shopper_numid cua Shopper quen thuoc
                    long shopper_numid = BizShopper.getShopperNumID(pShopper.by_na, pShopper.name, pShopper.nickname, pShopper.phone, pShopper.address);

                    //Tao thong tin cho Invoice
                    pInvoice.by_shopper = shopper_numid;
                    if (pNA.na_charid != "") //NA ban hang
                    {
                        pInvoice.by_na = pNA.na_charid;
                        pInvoice.by_shop = pNA.by_shop;
                    }
                    else //ShopOwner ban hang
                    {
                        pInvoice.by_na = "NADEFAULT";
                        pInvoice.by_shop = pNA.by_shop;
                    }

                    currInvoiceNumID = pInvoice.addInvoice();
                }

                //Tao chi tiet hoa don
                pInvoiceDetail.by_invoice = currInvoiceNumID;
                pInvoiceDetail.price = 0;
                pInvoiceDetail.addInvoiceDetail();
                return 1;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        #endregion
    }
}
